<?php

declare(strict_types=1);

namespace DreamCat\StateMachineTest\Cases\Config;

use DreamCat\StateMachine\Error\DefineConfigException;
use DreamCat\StateMachine\Factory\ConfigParseFactory;
use PHPUnit\Framework\TestCase;

/**
 * 单配置文件测试
 * @author vijay
 */
class SingleParseTest extends TestCase
{
    /**
     * 常规验证
     * @return void
     * @throws DefineConfigException
     */
    public function testNormal(): void
    {
        $dir = __DIR__ . "/../../resources/simpleSingle";
        $parse = ConfigParseFactory::getConfigParser("{$dir}/def.json");
        $def = $parse->parseConfig();
        self::assertEquals(file_get_contents("{$dir}/defineSeri.txt"), serialize($def));
    }

    /**
     * 测试错误的定义文件
     * @param string $fileName 文件名
     * @param string $errorMsg 异常信息
     * @return void
     * @dataProvider errorCases
     */
    public function testErrorFile(string $fileName, string $errorMsg): void
    {
        $this->expectException(DefineConfigException::class);
        $this->expectErrorMessage($errorMsg);
        $parse = ConfigParseFactory::getConfigParser(__DIR__ . "/../../resources/error/singleFile/{$fileName}.json");
        $parse->parseConfig();
    }

    /**
     * 错误定义的用例列表
     * @return array
     */
    public function errorCases(): array
    {
        return [
            [
                "duplicateRole",
                "存在重复的角色标识 customer",
            ],
            [
                "duplicateAction",
                "存在重复的动作标识 stop",
            ],
            [
                "NoExistRole",
                "动作 stop 的操作员 customer1 未定义",
            ],
            [
                "duplicateNode",
                "存在重复的节点标识 stop",
            ],
            [
                "NoExistAction",
                "节点 normal 的状态转移动作标识 stop1 不存在",
            ],
            [
                "switchNoTarget",
                "节点 normal 的状态转移 stop 的目标节点 st1 不存在",
            ],
            [
                "switchDuplicateTarget",
                "节点 normal 的状态转移 stop 有重复的条件判断",
            ],
            [
                "duplicateTransfer",
                "节点 normal 的状态转移有重复的动作标识 stop",
            ],
        ];
    }
}

# end of file
